package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.CpAthlete;
import com.ruoyi.system.domain.CpCountry;
import com.ruoyi.system.mapper.CpAthleteMapper;
import com.ruoyi.system.service.ICpAthleteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 参赛选手信息Service业务层处理
 *
 * @author zhangjinlei
 * @date 2022-04-25
 */
@Service
public class CpAthleteServiceImpl implements ICpAthleteService
{
    @Autowired
    private CpAthleteMapper cpAthleteMapper;

    /**
     * 查询参赛选手信息
     *
     * @param id 参赛选手信息主键
     * @return 参赛选手信息
     */
    @Override
    public CpAthlete selectCpAthleteById(Long id)
    {
        return cpAthleteMapper.selectCpAthleteById(id);
    }

    /**
     * 查询参赛选手信息列表
     *
     * @param cpAthlete 参赛选手信息
     * @return 参赛选手信息
     */
    @Override
    public List<CpAthlete> selectCpAthleteList(CpAthlete cpAthlete) throws Exception {
        List<CpCountry> list = cpAthleteMapper.selectCpCountryList(cpAthlete);
        if(CollectionUtils.isEmpty(list)){
            throw new Exception("没有参赛国家，请检查是否有参赛国家");
        }
         return cpAthleteMapper.selectCpAthleteList(cpAthlete);
    }

    /**
     * 新增参赛选手信息
     *
     * @param cpAthlete 参赛选手信息
     * @return 结果
     */
    @Override
    public int insertCpAthlete(CpAthlete cpAthlete) throws Exception {
        List<CpCountry> list = cpAthleteMapper.selectCpCountryList(cpAthlete);
        if(CollectionUtils.isEmpty(list)){
            throw new Exception("该国家不存在，请检查参赛国家");
        }
        CpCountry cpCountry=list.get(0);
        if ( cpCountry.getParticiPationStatus() == 1){
            throw new Exception("该国家处于禁用状态，请启用后再次添加");
        }
        cpAthlete.setCountryId(cpCountry.getId());
        cpAthlete.setCreateTime(DateUtils.getNowDate());
        return cpAthleteMapper.insertCpAthlete(cpAthlete);
    }

    /**
     * 修改参赛选手信息
     *
     * @param cpAthlete 参赛选手信息
     * @return 结果
     */
    @Override
    public int updateCpAthlete(CpAthlete cpAthlete)
    {
        cpAthlete.setUpdateTime(DateUtils.getNowDate());
        return cpAthleteMapper.updateCpAthlete(cpAthlete);
    }

    /**
     * 批量删除参赛选手信息
     *
     * @param ids 需要删除的参赛选手信息主键
     * @return 结果
     */
    @Override
    public int deleteCpAthleteByIds(Long[] ids)
    {
        return cpAthleteMapper.deleteCpAthleteByIds(ids);
    }

    /**
     * 删除参赛选手信息信息
     *
     * @param id 参赛选手信息主键
     * @return 结果
     */
    @Override
    public int deleteCpAthleteById(Long id)
    {
        return cpAthleteMapper.deleteCpAthleteById(id);
    }
}
